/**
 * 配置
 * */
package org.zmhhxl.user.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zmhhxl.api.auth.domain.entity.ThirdPartyUser;
import org.zmhhxl.common.core.domain.Result;
import org.zmhhxl.user.service.ThirdPartyUserService;

import java.util.Optional;

@RestController
@RequiredArgsConstructor
public class ThirdPartyUserController {
   private final ThirdPartyUserService thirdPartyUserService;

   @GetMapping("/user/third_party/find_by_id/{id}")
   public Result<Optional<ThirdPartyUser>> findById(@PathVariable("id") Long id) {
      return Result.ok(thirdPartyUserService.findById(id));
   }

   @GetMapping("/user/third_party/find_by_thirdparty_id/{domain:.+}/id/{id}")
   public Result<Optional<ThirdPartyUser>> findByThirdPartyIdAndThirdPartyDomain(@PathVariable("id") String id, @PathVariable("domain") String domain){
      return Result.ok(thirdPartyUserService.findByThirdPartyIdAndThirdPartyDomain(id, domain));
   }

   @GetMapping("/user/third_party/find_user_id_by_id/{id}")
   public Result<Optional<Long>> findUserIdById(@PathVariable("id") Long id) {
      return Result.ok(Optional.ofNullable(thirdPartyUserService.findById(id).map(ThirdPartyUser::getUserInfos).get().getId()));
   }

   @PostMapping("/user/third_party/save")
   public Result<Boolean> save(ThirdPartyUser thirdPartyUser) {
      return Result.ok((thirdPartyUserService.save(thirdPartyUser)!=null)?true:false);
   }

}
